package cn.hp.servlet;

import cn.hp.service.RoleService;
import cn.hp.service.RoleServiceImp;
import cn.hp.service.UserService;
import cn.hp.service.UserServiceImp;
import cn.hp.util.PageBean;
import cn.hp.util.ResultJson;
import cn.hp.util.ReturnJsonData;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 角色控制层
 * ajax ，并以json格式反会结果
 * 除了findById方法之外
 */
@WebServlet("/RoleServlet")
public class RoleServlet extends HttpServlet {
    private RoleService service= new RoleServiceImp();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp){
        String method = req.getParameter("method");
        if("find".equals(method)){
            find(req,resp);
        }else if("save".equals(method)){
            save(req,resp);
        }else if("upp".equals(method)){
            upp(req,resp);
        }else if("del".equals(method)){
            del(req,resp);
        }else if("findById".equals(method)){
            findById(req,resp);
        }else{
            System.out.println("未知访问："+method);
        }
    }

    private void findById(HttpServletRequest req, HttpServletResponse resp){
        Integer id = req.getParameter("id")!=null?Integer.valueOf(req.getParameter("id")):0;
        String sql = "select * from role where id=? ";
        Map map = service.findById(sql,id);
        ReturnJsonData.returnJson(map!=null?ResultJson.success(map):ResultJson.error(),resp);
    }

    private void del(HttpServletRequest req, HttpServletResponse resp){
        Integer id = req.getParameter("id")!=null?Integer.valueOf(req.getParameter("id")):0;
        String sql = "delete from role where id = ?";

        int i = service.del(sql,id);
        ReturnJsonData.returnJson(i>0?ResultJson.success():ResultJson.error(),resp);
    }

    private void save(HttpServletRequest req, HttpServletResponse resp){
        String roleType = req.getParameter("roleType");
        String state = req.getParameter("state");
        String sql = "insert into role(roleType,state) values(?,?)";
        int i = service.save(sql,roleType,state);
        ReturnJsonData.returnJson(i>0?ResultJson.success():ResultJson.error(),resp);
    }

    private void upp(HttpServletRequest req, HttpServletResponse resp){
        String id = req.getParameter("id");
        String roleType = req.getParameter("roleType");
        String state = req.getParameter("state");
        String sql = "update role set roleType = ?,state=? where id=?";
        int i = service.upp(sql,roleType,state,id);
        ReturnJsonData.returnJson(i>0?ResultJson.success():ResultJson.error(),resp);
    }

    /**
     * 查询
     */
    private void find(HttpServletRequest req, HttpServletResponse resp){
        Integer page = req.getParameter("page")!=null?Integer.valueOf(req.getParameter("page")):1;
        Integer limit = req.getParameter("limit")!=null?Integer.valueOf(req.getParameter("limit")):10;

        PageBean<Map> list = service.pageFind(page, limit, "select * from role");
        ReturnJsonData.returnJson(ResultJson.success(list),resp);
    }
}
